<?php

class View_Smarty{
    var $tpl;
    var $tplFile;
    function View_Smarty($tplFile = '')    {
        $smartyClass = SYSBASE.'Libraries/Smarty/Smarty.class.php';
        if(!file_exists($smartyClass)) showError(sprintf(__T("Can't find SMARTY library: %s"),$smartyClass));
        include($smartyClass);
        $this -> tpl = new Smarty();
        
        $Config =& Loader::config();

        $smarty_conf = $Config['smarty'];
        //默认设置
        $this->tpl->left_delimiter = $smarty_conf['left_delimiter'];
        $this->tpl->right_delimiter = $smarty_conf['right_delimiter'];
        $this->tpl->template_dir = $smarty_conf['template_dir'];
        $this->tpl->config_dir   = $smarty_conf['config_dir'];
        $this->tpl->compile_dir  = $smarty_conf['compile_dir'];
        array_unshift($this->tpl->plugins_dir,APPBASE.'/Libraries/smartyplugin');
        
        if(!empty($tplFile))
            $this -> tplFile = $tplFile; //模板文件
    }

    function display($tplFile = ''){
        if(!empty($tplFile))
            $this -> tplFile = $tplFile;

        echo $this -> fetch();
    }

    function fetch($tplFile = ''){
        if(!empty($tplFile))
            $this -> tplFile = $tplFile;

        $response =& Loader::lib('output');
        $this -> tpl -> assign($response->get());
        $this -> tpl -> register_function('siteurl','getSiteurl');
        $this -> tpl -> register_modifier('mycutstr','mycutstr');
        $this -> tpl -> register_block('t','smarty_block_t');
        return $this -> tpl -> fetch($this -> tplFile);
    }
}

function getSiteurl($params,&$smarty){
    extract($params);
    if(isset($uri) && empty($uri)){
        return base_url();
    }
    if(isset($uri) && !empty($uri)){
        return site_url($uri);
    }
    return base_url();
}

function mycutstr($string, $length, $dot = ' ...', $charset = 'utf-8') {

    if(strlen($string) <= $length) {
        return $string;
    }

    $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);

    $strcut = '';
    if(strtolower($charset) == 'utf-8') {

        $n = $tn = $noc = 0;
        while($n < strlen($string)) {

            $t = ord($string[$n]);
            if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
                $tn = 1; $n++; $noc++;
            } elseif(194 <= $t && $t <= 223) {
                $tn = 2; $n += 2; $noc += 2;
            } elseif(224 <= $t && $t <= 239) {
                $tn = 3; $n += 3; $noc += 2;
            } elseif(240 <= $t && $t <= 247) {
                $tn = 4; $n += 4; $noc += 2;
            } elseif(248 <= $t && $t <= 251) {
                $tn = 5; $n += 5; $noc += 2;
            } elseif($t == 252 || $t == 253) {
                $tn = 6; $n += 6; $noc += 2;
            } else {
                $n++;
            }

            if($noc >= $length) {
                break;
            }

        }
        if($noc > $length) {
            $n -= $tn;
        }

        $strcut = substr($string, 0, $n);

    } else {
        for($i = 0; $i < $length; $i++) {
            $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
        }
    }

    $strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);

    return $strcut.$dot;
}

function smarty_gettext_strarg($str)
{
    $tr = array();
    $p = 0;

    for ($i=1; $i < func_num_args(); $i++) {
        $arg = func_get_arg($i);
        
        if (is_array($arg)) {
            foreach ($arg as $aarg) {
                $tr['%'.++$p] = $aarg;
            }
        } else {
            $tr['%'.++$p] = $arg;
        }
    }
    
    return strtr($str, $tr);
}

function smarty_block_t($params, $text, &$smarty)
{
    $text = stripslashes($text);
    
    // set escape mode
    if (isset($params['escape'])) {
        $escape = $params['escape'];
        unset($params['escape']);
    }
    
    // set plural version
    if (isset($params['plural'])) {
        $plural = $params['plural'];
        unset($params['plural']);
        
        // set count
        if (isset($params['count'])) {
            $count = $params['count'];
            unset($params['count']);
        }
    }
    
    // use plural if required parameters are set
    if (isset($count) && isset($plural)) {
        $text = _ngettext($text, $plural, $count);
    } else { // use normal
        $text = _gettext($text);
    }

    // run strarg if there are parameters
    if (count($params)) {
        $text = smarty_gettext_strarg($text, $params);
    }

    if (!isset($escape) || $escape == 'html') { // html escape, default
       $text = nl2br(htmlspecialchars($text));
   } elseif (isset($escape)) {
        switch ($escape) {
            case 'javascript':
            case 'js':
                // javascript escape
                $text = str_replace('\'', '\\\'', stripslashes($text));
                break;
            case 'url':
                // url escape
                $text = urlencode($text);
                break;
        }
    }
    
    return $text;
}

?>